﻿using CPPEI.WebApi;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CMS.WebApi.article
{
    [AuthGetAction("article", "getTop10", "1.0")]
    public class getTop10Action : BaseAction<getTop10Params, getTop10Result>
    {
        protected override ActionResult<getTop10Result> OnExecute(ActionContext context, SysParams sysParams, getTop10Params opParams)
        {
            CMS.Domain.Services.QueryService service = new Domain.Services.QueryService();
            CPPEI.PagedResult<CMS.Domain.Entities.EArticle> serResult = service.QueryPublishedTop10Articles(0, 10);

            if(serResult.IsSuccess)
            {
                getTop10Result actResult = new getTop10Result();
                foreach (CMS.Domain.Entities.EArticle article in serResult.Data)
                {
                    getTop10Result.Item item = new getTop10Result.Item();
                    item.Id = article.Id;
                    item.Title = article.Title;
                    item.CreatedTime = article.CreatedTime;
                    item.PublishedUrl = string.Format("{0}{1}", CMS.Domain.Cache.BaseUrl, article.PublishedUrl);

                    if (article.PublishedTime != null)
                    {
                        item.PublishedTime = article.PublishedTime.Value.ToString("yyyy-MM-dd");
                    }
                    else
                    {
                        item.PublishedTime = string.Empty;
                    }

                    actResult.Items.Add(item);
                }
                return new ActionResult<getTop10Result>(actResult);
            }
            else
            {
                return new ActionResult<getTop10Result>(serResult);
            }
        }
    }

    public class getTop10Params
    {
    }

    public class getTop10Result
    {

        public List<Item> Items = new List<Item>();

        public class Item
        {
            public Guid Id { get; set; }

            public string Title { get; set; }

            public DateTime CreatedTime { get; set; }

            public string PublishedUrl { get; set; }

            public string PublishedTime { get; set; }
        }
    }
}
